package com.wontube.smr.cron;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wontube.core.cron.IJobs;
import com.wontube.smr.bo.sites.VerifyUrlBO;

/**
 * @author yuechao
 *
 */
public class VerifyUrlJobs implements IJobs {
	private static final Logger logger = LoggerFactory.getLogger(VerifyUrlJobs.class);
	
	/* 
	 * @see com.wontube.core.cron.IJobs#name()
	 */
	@Override
	public String name() {
		return "verify-url";
	}

	/* 
	 * @see com.wontube.core.cron.IJobs#execute(java.lang.String[])
	 */
	@Override
	public IJobs execute(String[] args) {
		// 防止多次指定相同的任务
		for (int i = 0; i < args.length - 1; i++) {
			String arg = args[i];
			if ("-tv".equals(arg)) {
				String dateDiff = args[i + 1];
				int diff = Integer.parseInt(dateDiff);
				this.verifyTV(diff);
			} else if ("-movie".equals(arg)) {
				String dateDiff = args[i + 1];
				int diff = Integer.parseInt(dateDiff);
				this.verifyMovie(diff);
			}
		}
		
		return this;
	}

	private void verifyTV(int dateDiff) {
		long start = System.currentTimeMillis();
		int count = VerifyUrlBO.cleanupTV(dateDiff);
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("verify {} tv or episode items, cost {} s", count, cost);
	}
	
	private void verifyMovie(int dateDiff) {
		long start = System.currentTimeMillis();
		int count = VerifyUrlBO.cleanupMovie(dateDiff);
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("verify {} movie items, cost {} s", count, cost);
	}
}
